/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal.spc;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.ex.CheckBoxHeadModal;
import com.intelligent.ispc.core.dto.TestItemDto;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by tony.yu on 2016/8/26.
 */
public class TestItemSimpleModel extends AbstractTableModel implements CheckBoxHeadModal {
    private String[] columns;
    private List<TestItemDto> data;

    private CheckChangeEvent changeEvent;
    private int[] viewData;

    /**
     * Constructor
     *
     * @param columns columns
     */
    public TestItemSimpleModel(String[] columns) {
        this.columns = columns;
        data = Lists.newArrayList();
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data == null || data.isEmpty()) {
            return new Object();
        }
        TestItemDto testItemDto = data.get(rowIndex);
        Boolean check = testItemDto.getCheck();
        String name = testItemDto.getName();
        switch (columnIndex) {
            case 0:
                return check == null ? true : check;
            case 1:
                return name;
            default:
                return "";
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        TestItemDto testItemDto = data.get(rowIndex);

        switch (columnIndex) {
            case 0:
                testItemDto.setCheck((Boolean) aValue);
                break;
            case 1:
                testItemDto.setName((String) aValue);
                break;
            default:
                break;
        }
    }

    /**
     * Set data
     *
     * @param dtos testItemDto list
     */
    public void setData(List<TestItemDto> dtos) {
        if (dtos == null) {
            data = Lists.newArrayList();
        }
        data = dtos;
        fireTableDataChanged();
    }

    /**
     * Set checked  or not
     *
     * @param aValue   true or false
     * @param rowIndex which line
     */
    public void setCheckValue(boolean aValue, int rowIndex) {
        data.get(rowIndex).setCheck(aValue);
        fireTableDataChanged();
    }

    public List<TestItemDto> getData() {
        return data;
    }

    /**
     * Get the selected count
     *
     * @return count
     */
    public int getSelectedCount() {
        int checkedCount = 0;
        for (TestItemDto dto : data) {
            if (dto.getCheck()) {
                checkedCount++;
            }
        }
        return checkedCount;
    }

    /**
     * Whether select all or not
     *
     * @param value true or false
     */
    public void selectAll(boolean value) {
        if (viewData != null) {
            for (int i = 0; i < viewData.length; i++) {
                this.setValueAt(value, viewData[i], 0);
            }
        }

        if (changeEvent != null) {
            changeEvent.change();
        }

        fireTableDataChanged();
    }

    /**
     * Check change
     *
     * @param e CheckChangeEvent
     */
    public void onCheckChange(CheckChangeEvent e) {
        this.changeEvent = e;
    }

    /**
     * CheckChangeEvent interface
     */
    public interface CheckChangeEvent {
        /**
         * Declar method of change()
         */
        void change();
    }

    public void setViewData(int[] viewData) {
        this.viewData = viewData;
    }

}
